import { Router } from 'express'
import MinArticleDao from '../dao/MinArticleDao.js'
import MinArticle from '../verify/MinArticle.js'
import File from '../util/File.js'
import path from 'path'

let router = Router()

router.route('/api/min_article')
    .post(async (req, res) => {
        req.body.user_id = 1
        let min_article = MinArticle.insertObj(req.body)
        if (!min_article) return res.end(JSON.stringify({ code: 500, msg: '参数有误' }))

        let result = await MinArticleDao.insert(min_article)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        //删除所有临时文章封面图
        File.delete(req.session.files, req.body.files)
        req.session.files = []

        return res.end(JSON.stringify({ code: 200 }))
    })

    .delete(async (req, res) => {
        let min_article = MinArticle.deleteObj(req.body)
        if (!min_article) return res.end(JSON.stringify({ code: 500, msg: '参数有误' }))

        let { files } = await MinArticleDao.selectById(article.id)

        let result = await MinArticleDao.delete(min_article)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        //删除文章封面图
        for (const file of files) {
            let urls = file.split('/')
            let old_url = path.join(path.resolve(), '/public/assets', 'img', urls[urls.length - 1])
            req.session.files.push(old_url)
        }
        File.delete(req.session.files)
        req.session.files = []

        return res.end(JSON.stringify({ code: 200 }))
    })

    .put(async (req, res) => {
        let min_article = MinArticle.updateObj(req.body)
        if (!min_article) return res.end(JSON.stringify({ code: 500, msg: '参数有误' }))

        let { files } = await MinArticleDao.selectById(min_article.id)

        let result = await MinArticleDao.update(min_article)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        //删除所有临时文章封面图
        for (const file of files) {
            let urls = file.split('/')
            let old_url = path.join(path.resolve(), '/public/assets', 'img', urls[urls.length - 1])
            req.session.files.push(old_url)
        }
        File.delete(req.session.files, req.body.files)
        req.session.files = []

        return res.end(JSON.stringify({ code: 200 }))
    })

router
    .get('/api/min_article/:id', async (req, res) => {
        let result = await MinArticleDao.selectById(req.params.id)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200, data: result }))
    })

    .get('/api/min_articleByPage/:pageIndex/:pageSize', async (req, res) => {
        let { pageIndex, pageSize } = req.params

        let result = await MinArticleDao.selectByLimit((pageIndex - 1) * pageSize, pageSize)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200, data: result }))
    })

    .get('/api/min_articleTotal', async (req, res) => {
        let result = await MinArticleDao.selectCount()
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200, data: result }))
    })

export default router